package com.temp.controller.admin;

import com.temp.dto.RouteDTO;
import com.temp.entity.Route;
import com.temp.result.Result;
import com.temp.service.RouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/admin/route")
@Api(tags = "管理路由相关接口")
@Slf4j
@Validated
public class AdminRouteController {

    @Autowired
    private RouteService routeService;

    @GetMapping("/query")
    @ApiOperation("获取路由信息")
    @RequiresPermissions("admin:route:query")
    public Result getAllRoute() {
        return Result.success(routeService.findAllAuthorizedRoutes(),"apiMsg.querySuccess");
    }

    @GetMapping("/list")
    @ApiOperation("获取全部可用路由")
    @RequiresPermissions("admin:route:list")
    public Result getListRoute() { return Result.success(routeService.findListRoutes(),"apiMsg.querySuccess");}

    @PostMapping("/updateOrSave")
    @ApiOperation("保存或者更新路由")
    @RequiresPermissions("admin:route:updateOrSave")
    public Result updateOrSave(@Valid @RequestBody RouteDTO routeDTO) {
        Route route = new Route();
        BeanUtils.copyProperties(routeDTO, route);
        boolean isSuccess = routeService.saveOrUpdate(route);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除路由")
    @RequiresPermissions("admin:route:delete")
    public Result delRoute(@RequestParam Long id) {
        boolean isSuccess = routeService.removeById(id);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }
}
